package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.ExpressService;
import com.cskaoyan.service.ExpressServiceImpl;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.OrderServiceImpl;
import com.cskaoyan.util.*;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author stone
 * @date 2023/03/17 19:17
 */
@WebServlet("/admin/order/*")
public class AdminOrderServlet extends HttpServlet {

    private OrderService orderService = new OrderServiceImpl();
    private ExpressService expressService = new ExpressServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = URIUtil.getOperation(this.getClass(), request);
        switch (operation) {
            case "/list":
                list(request, response);
                break;
            case "/detail":
                detail(request, response);
                break;
            case "/refund":
                refund(request, response);
                break;
            case "/channel":
                channel(request, response);
                break;
            case "/ship":
                ship(request, response);
                break;
            case "/delete":
                delete(request, response);
                break;
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Integer orderId = JacksonUtil.parseInteger(jsonStr, "orderId");
        ResponseUtil.responseJson(response,orderService.remove(orderId));
    }

    private void ship(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Integer orderId = JacksonUtil.parseInteger(jsonStr, "orderId");
        String shipSn = JacksonUtil.parseString(jsonStr, "shipSn");
        String shipChannel = JacksonUtil.parseString(jsonStr, "shipChannel");
        ResponseUtil.responseJson(response,orderService.ship(orderId,shipSn,shipChannel));
    }

    private void channel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResponseUtil.responseOkJsonVo(response,expressService.getVendors());
    }

    private void refund(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Integer orderId = JacksonUtil.parseInteger(jsonStr, "orderId");
        String refundMoney = JacksonUtil.parseString(jsonStr, "refundMoney");
        BaseRespVo vo = orderService.refund(orderId,refundMoney);
        ResponseUtil.responseJson(response,vo);
    }

    private void detail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Map<String, Object> data = orderService.detail(id);
        ResponseUtil.responseOkJsonVo(response,data);
    }

    @SneakyThrows
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = request.getParameter("userId") == null ? null : Integer.parseInt(request.getParameter("userId"));
        String orderSn = request.getParameter("orderSn");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(WdConstant.DEFAULT_DATETIME_PATTERN);
        //LocalDateTime start = LocalDateTime.parse(request.getParameter("start"), formatter);
        //LocalDateTime end = LocalDateTime.parse(request.getParameter("end"), formatter);
        SimpleDateFormat df = new SimpleDateFormat();
        Date start = StringUtil.isEmpty(request.getParameter("start")) ? null : df.parse(request.getParameter("start"));
        Date end = StringUtil.isEmpty(request.getParameter("end")) ? null : df.parse(request.getParameter("end"));
        List<Short> orderStatusArray = new ArrayList<>();
        String[] orderStatusArrays = request.getParameterValues("orderStatusArray");
        if (!ArrayUtil.isEmpty(orderStatusArrays)) {
            orderStatusArray = Arrays.asList(orderStatusArrays).stream().map(Short::valueOf).collect(Collectors.toList());
        }
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        CommonData<MarketOrder> commonData = orderService.list(userId, orderSn, start, end, orderStatusArray, page, limit, sort, order);
        ResponseUtil.responseJson(response, BaseRespVo.ok(commonData));
    }
}
